package manage.service;

import manage.dao.StudentDao;
import manage.domain.Student;

public class StudentService {

    private StudentDao studentDao = new StudentDao();

    //判断用户输入的Id在学生数组中是否存在对应的学生信息
    public boolean isExists(String id) {
        Student[] stus = studentDao.findAllStudent();//调用findAllStudent方法获取所有学生对象
        boolean flag = false;
        for (int i = 0; i < stus.length; i++) {
            Student s = stus[i];
            if (s != null && s.getId().equals(id)) {//判断用户输入的id在student数组中是否有存在的
                flag = true;
                break;
            }
        }
        return flag;
    }

    //向studentDao传递想要添加的学生对象
    public boolean addStudent(Student stu) {

        return studentDao.addStudent(stu);
    }

    //查找数组中的所有学生信息
    public Student[] findAllStudent() {
        Student[] allStudent = studentDao.findAllStudent();
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
            Student stus = allStudent[i];
            if (stus != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return allStudent;
        } else {
            return null;
        }

    }

    //向studentDao传递用户所想要删除的学生Id
    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    //向studentDao传递用户要修改的学生信息id和想要修改的学生对象
    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId, newStu);
    }
}
